package com.demo.function.mybatis.mybatisplus.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.mapper.TenantMapper;
import com.demo.model.entity.Tenant;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TenantService extends ServiceImpl<TenantMapper, Tenant> {

    @Resource
    private TenantMapper tenantMapper;

    /**
     * 自动添加租户id
     * @param name 名称
     * @return 租户数据集合
     */
    public List<Tenant> query(String name) {
        LambdaQueryWrapper<Tenant> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Tenant::getName, name);

        return tenantMapper.selectList(wrapper);
    }

    /**
     * 不使用租户id查询
     * @param name 名称
     * @return 租户数据集合
     */
    public List<Tenant> queryWithoutTenant(String name) {
        return tenantMapper.queryWithoutTenant(name);
    }

    public List<Tenant> saveBatchByParam(int totalRecord) {
        List<Tenant> list = new ArrayList<>();
        for (long i = 0; i < totalRecord; i++) {
            Tenant tenant = new Tenant();
            tenant.setName("name" + i);
            tenant.setTenantId(i);
            list.add(tenant);
        }
        super.saveBatch(list);
        return list;
    }
}
